﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace He.Common
{
    /// <summary>
    /// 验证辅助类(验证器)
    /// </summary>
    public class Validator
    {
        /// <summary>
        /// 判断给定的字符串(strInt)是否是正整数
        /// </summary>
        /// <param name="strInt">要确认的字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsInt(string strInt)
        {
            return new Regex(@"^(0|[1-9]\d*)$").IsMatch(strInt);	//正整数
        }

        /// <summary>
        /// 判断给定的字符串(strNumeral)是否是数字(整数或小数)
        /// </summary>
        /// <param name="strNumeral">要确认的字符串</param>
        /// <returns></returns>
        public static bool IsNumeric(string strNumeral)
        {
            return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumeral);	//整数和小数
        }




        /// <summary>
        /// 检测是否符合email格式
        /// </summary>
        /// <param name="strEmail">要判断的email字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsEmail(string strEmail)
        {
            return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        /// <summary>
        /// 验证验证码
        /// </summary>
        /// <param name="strcode"></param>
        /// <returns></returns>
        public static string VerifyCode(string strcode)
        {
            if (string.IsNullOrEmpty(strcode))
            {
                return "对不起，请输入验证码！";
            }
            if (HttpContext.Current.Session[SysKeys.VERIFY_SESSION] == null)
            {
                return "对不起，验证码超时或已过期！";
            }
            if (strcode.ToLower() != (HttpContext.Current.Session[SysKeys.VERIFY_SESSION].ToString()).ToLower())
            {
                return "您输入的验证码与系统的不一致！";
            }
            return "success";
        }
    }
}
